// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import {
	postlang
} from '@/api/all.js';

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		userinfo: uni.getStorageSync('userinfo') ? JSON.parse(uni.getStorageSync('userinfo')) : '',
		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
		uid: uni.getStorageSync('userinfo') ? JSON.parse(uni.getStorageSync('userinfo')).id : '01',
		lang: uni.getStorageSync('lang') ? uni.getStorageSync('lang') : 'cn',
		location: uni.getStorageSync('location') ? JSON.parse(uni.getStorageSync('location')) : '',
		address: uni.getStorageSync('address') ? JSON.parse(uni.getStorageSync('address')) : '',
		top_id: uni.getStorageSync('top_id') ? uni.getStorageSync('top_id') : '',
		shipment: 0,
		shop: {
			totalprice: 0,
			totalnumber: 0,
			totalshop: []
		},
		keyboard: false,
		//默认地址
		useraddress: [],
		//底部导航索引
		current: 0
	},
	getters: {
		getuserinfo(state) {
			return state.userinfo
		},
		gettoken(state) {
			return state.token
		},
		getlocation(state) {
			return state.location
		},
		getaddress(state) {
			return state.address
		},
		gettotalprice(state) {
			return state.shop.totalprice
		},
		gettotalshop(state) {
			return state.shop.totalshop
		},
		gettopid(state) {

			return state.top_id
		},
		//获取默认地址
		getuseraddress(state) {

			return state.useraddress
		},
		getshipment(state) {
			return state.shipment
		},
		getcurrent(state) {
			return state.current
		},




	},
	mutations: {
		//键盘监听--H5键盘有问题
		setKeyboard(state, keyboard) {
			state.keyboard = keyboard;
		},
		//用户信息
		setUserinfo(state, e) {
			uni.setStorageSync('userinfo', JSON.stringify(e))
			state.userinfo = e;
		},
		//token
		setUsertoken(state, e) {
			uni.setStorageSync('token', e)
			state.token = e;
		},
		//语言
		setLang(state, lang) {
			uni.setStorageSync('lang', lang)
			state.lang = lang;
			if (state.token) {
				postlang({
					language: lang == 'ug' ? 'ug' : 'zh_CN'
				}).then((res) => {
					if (res.code == 200) {}
				})
			}

		},
		//地址
		setLocation(state, location) {
			uni.setStorageSync('location', JSON.stringify(location))
			state.location = location;
		},
		//地址
		setAddress(state, address) {
			uni.setStorageSync('address', JSON.stringify(address));
			state.address = address;
		},
		setTotalprice(state, prams) {

			let newDatalist = state.shop.totalshop.find(item => item.id === prams.shopid).categories[prams
				.index].products.map(_item => {
				if (_item.id == prams.item.id) {
					if (!_item.qty) {
						_item.qty = 1;
						state.shop.totalprice += prams.item.price;
						state.shop.totalnumber++;
					} else {
						if (prams.symbol == '+') {
							_item.qty += 1;
							state.shop.totalprice += prams.item.price;
							state.shop.totalnumber++;
						} else {
							_item.qty -= 1;
							state.shop.totalprice -= prams.item.price;
							state.shop.totalnumber--;

						}
					}

				}
				return _item;
			})
			state.shop.totalshop.find(item => item.id === prams.shopid).categories[prams.index].products =
				newDatalist;

		},
		setTotalshop(state, item) {
			state.shop.totalshop.push(item)
		},
		clearShop(state){
			state.shop.totalprice=0;
			state.shop.totalnumber=0;
		},
		setTopid(state, id) {
			uni.setStorageSync('top_id', id)
			state.top_id = id;
		},
		//修改默认地址
		setUseraddress(state, useraddress) {
			state.useraddress = useraddress;
		},
		//获取配送费
		setShipment(state, shipment) {
			state.shipment = shipment;
		},
		//修改底部导航
		setCurrent(state, current) {
			state.current = current;
		}
	}
})
export default store